package com.shenzhen.horselive.ui.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.flqy.baselibrary.utils.ScreenUtil;
import com.gyf.immersionbar.ImmersionBar;
import com.king.zxing.util.CodeUtils;
import com.shenzhen.horselive.R;
import com.shenzhen.horselive.User;
import com.shenzhen.horselive.api.ApiFactory;
import com.shenzhen.horselive.base.BaseActivity;
import com.shenzhen.horselive.beans.CommResponseBean;
import com.shenzhen.horselive.beans.CommResponseDataBean;
import com.shenzhen.horselive.beans.HongBaoRangeBean;
import com.shenzhen.horselive.databinding.ActivityShareBinding;
import com.shenzhen.horselive.network.SchedulersCompat;
import com.shenzhen.horselive.presenter.LoadingViewObserver;
import com.shenzhen.horselive.ui.dialog.ShareDialog;
import com.shenzhen.horselive.utils.A;
import com.shenzhen.horselive.utils.ImageUtils;
import com.shenzhen.horselive.utils.ScreenShot;
import com.shenzhen.horselive.wx.WXManager;
import com.yalantis.ucrop.util.BitmapLoadUtils;

public class ShareActivity extends BaseActivity {

    ActivityShareBinding  binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityShareBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        ImmersionBar.with(this)
                .init();
        initViews();
        initData();

    }

    private void initData() {
        ApiFactory.getApiService().getShareurl(User.get().getMyInfo().getToken())
                .compose(SchedulersCompat.applyIoSchedulers())
                .safeSubscribe(new LoadingViewObserver<CommResponseDataBean>(this) {
                    @Override
                    protected void onNextInActive(CommResponseDataBean commResponseBean) {
                        if (commResponseBean.isSuccess())
                        {
                            String   url = commResponseBean.getData();
                            if (!TextUtils.isEmpty(url))
                            {
                                //转换成二维码
                                coedeIntoImageView(url+"?phone="+User.get().getMyInfo().getPhone());
                            }
                        }
                        else
                        {
                            showToast(commResponseBean.getErrMessage());
                        }
                    }
                });

        //获取自己的范围
     ApiFactory.getApiService().getHongBaoRange(User.get().getMyInfo().getToken())
             .compose(SchedulersCompat.applyIoSchedulers())
             .safeSubscribe(new LoadingViewObserver<HongBaoRangeBean>(this) {
                 @Override
                 protected void onNextInActive(HongBaoRangeBean hongBaoRangeBean) {
                     if (hongBaoRangeBean.isSuccess())
                     {
                         binding.tvTitle.setText(hongBaoRangeBean.getData()+"米");
                     }else
                     {
                        showToast(hongBaoRangeBean.getErrMessage());
                     }
                 }
             });

    }


    private void coedeIntoImageView(String url) {
        //生成二维码
       Bitmap  bitmap =  CodeUtils.createQRCode(url,200);
       if (bitmap!=null)

       {
           Bitmap bitmap1 = ((BitmapDrawable)getResources().getDrawable(R.mipmap.ico_share_backgroud)).getBitmap();
          A.QRCodeCompose  codeCompose = new A.QRCodeCompose();
           Bitmap  bitmap2 =   codeCompose.composeQrCodeBitmapByTransparentArea(bitmap1,bitmap, A.TransparentScanDirection.左上方);
          binding.imgImg.setImageBitmap(bitmap2);
       }
//        binding.imgCode.setImageBitmap(bitmap);






    }

    private void initViews() {
        binding.imgBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        binding.btGo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showShereDialog();
            }
        });
    }

    ShareDialog  dialog;
    //分享弹窗
    private void showShereDialog() {
        if (dialog==null)
        {
        dialog = new ShareDialog(getContext());
            dialog.setOnClick(new ShareDialog.OnClick() {
                @Override
                public void onDuihua() {
                    WXManager.getInstance().sharePictureToDuihua(ScreenShot.takeViewShot(binding.imgImg));
                }
                @Override
                public void onQuan() {
                    WXManager.getInstance().sharePictureToQuan(ScreenShot.takeViewShot(binding.imgImg));

                }
            });
        }
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();


    }
}